<?php

function recursive_mkdir($path, $mode = 0775, $uid = "", $gid = "") {
	$dirs = explode(DIRECTORY_SEPARATOR , $path);
	$count = count($dirs);
	$path = "";
	for ($i = 0; $i < $count; ++$i) {
		$path = $path.DIRECTORY_SEPARATOR.$dirs[$i];
		if (!@is_dir($path)) {
			if (!@mkdir($path))
				return false;
			if ($uid != "" && ! empty($uid)) chown($path, $uid);
			if ($gid != "" && ! empty($gid)) chgrp($path, $gid);
			chmod($path, $mode);
		}
	}
	return true;
}

function remove_directory($dir_to_remove, $remove_only_subdirs = false) {

	if (WWW_DIR_BASE === "" || !defined(WWW_DIR_BASE)) {
		return false;
	}

	if (CODE_BASE_DIR === "" || !defined(CODE_BASE_DIR)) {
		return false;
	}

	if (strstr($dir_to_remove, WWW_DIR_BASE) === false &&
		strstr($dir_to_remove, CODE_BASE_DIR) === false) {
		return false;
	}

	if (!is_file($dir_to_remove) && !is_dir($dir_to_remove))
		return true;

	return __recursive_rmdir($dir_to_remove, $remove_only_subdirs);
}

// DOT NOT USE THIS FUNCTION - USE ABOVE ONE
function __recursive_rmdir($path, $remove_only_subdirs = false) { //derived from php.net
	if (@is_dir($path)) {
		if (version_compare(PHP_VERSION, '5.0.0') < 0) {
			$entries = array();
			if ($handle = opendir($path)) {
				while (false !== ($file = readdir($handle)))
					$entries[] = $file;
				closedir($handle);
			}
		} else {
			$entries = scandir($path);
			if ($entries === false)
				$entries = array(); // just in case scandir fail...
		}

		foreach ($entries as $entry) {
			if ($entry != '.' && $entry != '..') {
				__recursive_rmdir($path.DIRECTORY_SEPARATOR.$entry, false);
			}
		}

		if ($remove_only_subdirs == false) {
			return rmdir($path);
		}
	} else {
		return unlink($path);
	}
}

function recursive_chown_chgrp($path, $uid, $gid, $recursive = true) {
	$d = opendir ($path) ;
	while(($file = readdir($d)) !== false) {
		if ($file != "." && $file != "..") {
			$fullpath = $path.DIRECTORY_SEPARATOR.$file ;

			if (filetype ($fullpath) == 'dir' && $recursive == true) {
				recursive_chown_chgrp ($fullpath, $uid, $gid);
			}

			chown($fullpath, $uid);
			chgrp($fullpath, $gid);
		}
	}
}

function chmod_chown($path, $dir_mode = 0775, $file_mode = 0664, $uid = "", $gid = "") {
	if (@is_dir($path)) {
		if ($uid != "" && ! empty($uid)) chown($path, $uid);
		if ($gid != "" && ! empty($gid)) chgrp($path, $gid);
		chmod($path, $dir_mode);
	} else {
		if ($uid != "" && ! empty($uid)) chown($path, $uid);
		if ($gid != "" && ! empty($gid)) chgrp($path, $gid);
		chmod($path, $file_mode);
	}
}

function recursive_chmod_chown($path, $dir_mode = 0775, $file_mode = 0664, $uid = "", $gid = "", $recursive = true) {
	if (version_compare(PHP_VERSION, '5.0.0') < 0) {
		$entries = array();
		if ($handle = opendir($path)) {
			while (false !== ($file = readdir($handle)))
				$entries[] = $file;
			closedir($handle);
		}
	} else {
		$entries = scandir($path);
		if ($entries === false)
			$entries = array(); // just in case scandir fail...
	}

	chmod_chown($path, $dir_mode, $file_mode, $uid, $gid);

	foreach ($entries as $entry) {
		if ($entry != '.' && $entry != '..') {
			$fullpath = $path.DIRECTORY_SEPARATOR.$entry;

			chmod_chown($fullpath, $dir_mode, $file_mode, $uid, $gid);

			if (filetype ($fullpath) == 'dir' && $recursive == true) {
				recursive_chmod_chown($fullpath, $dir_mode, $file_mode, $uid, $gid);
			}
		}
	}
}

function find_files_in_directory($dir_to_check, &$files_to_examine) {

	$items_in_dir = array();
	if ($handle = opendir($dir_to_check)) {
		while (false !== ($file = readdir($handle)))
			$items_in_dir[] = $file;
		closedir($handle);
	}

	foreach ($items_in_dir as $something) {
		if ($something != '.' && $something != '..') {
			$entity = $dir_to_check.DIRECTORY_SEPARATOR.$something;

			if (is_dir($entity)) {
				find_files_in_directory($entity, $files_to_examine);
			}

			if (is_file($entity)) {
				if (strstr($entity, ".svn"))
					continue;

				$_image_info = @getimagesize($entity);
				if (is_array($_image_info))
					continue;

				array_push($files_to_examine, $entity);
			}
		}
	}

}

?>
